package com.lc.recovery.ui.activity.recoverymine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.lc.base.BaseDialog;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.glide.GlideApp;
import com.lc.recovery.manager.ActivityManager;
import com.lc.recovery.manager.CacheDataManager;
import com.lc.recovery.manager.ThreadPoolManager;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.ui.activity.AccountSafeActivity;
import com.lc.recovery.ui.activity.HomeActivity;
import com.lc.recovery.ui.activity.RechargeActivity;
import com.lc.recovery.ui.dialog.MessageDialog;
import com.lc.recovery.util.SPUtil;
import com.lc.widget.layout.SettingBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RecoverySettingActivity extends AppActivity {
    @BindView(R.id.my_pic)
    ImageView myPic;
    @BindView(R.id.tv_shopname)
    TextView tvShopname;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.sb_person_music)
    SettingBar sbPersonMusic;
    @BindView(R.id.sb_setting_cache)
    SettingBar sbSettingCache;
    @BindView(R.id.sb_setting_about)
    SettingBar sbSettingAbout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recoverysetting;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        // 获取应用缓存大小
        sbSettingCache.setRightText(CacheDataManager.getTotalCacheSize(this));

    }

    @Override
    protected void onStart() {
        super.onStart();
        GlideApp.with(getActivity())
                .load( SPUtil.get(getContext(), IntentKey.IMAGE, ""))
                .placeholder(R.mipmap.avatar_placeholder_ic)
                .error(R.mipmap.avatar_placeholder_ic)
                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                .into(myPic);

        tvShopname.setText(SPUtil.get(getContext(), IntentKey.NAME, ""));
        tvUsername.setText(SPUtil.get(getContext(), IntentKey.PHONE, ""));
    }

    @OnClick({R.id.rl_personl_set, R.id.sb_person_music, R.id.sb_setting_cache, R.id.sb_setting_about, R.id.rl_outapp,R.id.sb_account_safe,R.id.sb_setting_xieyi,R.id.sb_setting_tiaokuan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sb_setting_xieyi:
            startActivity(new Intent(this,RechargeActivity.class).putExtra("title","用户协议").putExtra("Url","https://hyq.hljjrysm.com/xieyi.html"));
            break;
            case R.id.sb_setting_tiaokuan:
                startActivity(new Intent(this,RechargeActivity.class).putExtra("title","隐私条款").putExtra("Url","https://hyq.hljjrysm.com/yinsi.html"));
                break;
            case R.id.rl_personl_set:
                startActivity(PersonalCenterActivity.class);
                break;
            case R.id.sb_account_safe:
                startActivity(AccountSafeActivity.class);
                break;
            case R.id.sb_person_music:
                break;
            case R.id.sb_setting_cache:
                // 清除内存缓存（必须在主线程）
                GlideApp.get(getActivity()).clearMemory();
                ThreadPoolManager.getInstance().execute(() -> {
                    CacheDataManager.clearAllCache(this);
                    // 清除本地缓存（必须在子线程）
                    GlideApp.get(getActivity()).clearDiskCache();
                    post(() -> {
                        // 重新获取应用缓存大小
                        sbSettingCache.setRightText(CacheDataManager.getTotalCacheSize(getActivity()));
                    });
                });

                break;
            case R.id.sb_setting_about:
                startActivity(AboutActivity.class);
                break;
            case R.id.rl_outapp:
                // 消息对话框
                new MessageDialog.Builder(getActivity())
                        // 标题可以不用填写
                        .setTitle("退出登录")
                        // 内容必须要填写
                        .setMessage("您是否要退出登录？")
                        // 确定按钮文本
                        .setConfirm(getString(R.string.common_confirm))
                        // 设置 null 表示不显示取消按钮
                        .setCancel(getString(R.string.common_cancel))
                        // 设置点击按钮后不关闭对话框
                        //.setAutoDismiss(false)
                        .setListener(new MessageDialog.OnListener() {

                            @Override
                            public void onConfirm(BaseDialog dialog) {
                                // 进行内存优化，销毁除登录页之外的所有界面
                                ActivityManager.getInstance().finishAllActivities();
                                startActivity(HomeActivity.class);

//                                SPUtil.remove(getContext(), IntentKey.TYPE);
                                SPUtil.remove(getContext(), IntentKey.USERID);
                                SPUtil.remove(getContext(), IntentKey.BUSINESSUSERID);
                                SPUtil.remove(getContext(), IntentKey.IMAGE);
                                myPic.setImageResource(R.mipmap.mine_default_avatar);
                            }

                            @Override
                            public void onCancel(BaseDialog dialog) {

                            }
                        })
                        .show();

                break;
        }
    }
}
